// DirectClient.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <winioctl.h>

#define IOCTL_READ_DEVICE_INFO CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)

int main(int argc, char* argv[])
{
	HANDLE hdevice = CreateFile("\\\\.\\PSDODIRDVC", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (hdevice == INVALID_HANDLE_VALUE)
	{
		printf("Unable to open PSEUDODEVICE device - error %d\n", GetLastError());
		return 1;
	}

	wchar_t answer[512] = {'\0'};
	char buffer[1024] = {'\0'};
	DWORD junk;
	if (DeviceIoControl(hdevice, IOCTL_READ_DEVICE_INFO, NULL, 0, answer, sizeof(answer), &junk, NULL))
	{
		answer[junk] = 0;
		//MessageBoxW(NULL,answer, NULL, 0);
		wprintf(L"%s\r\n",answer);
	} else
		printf("Error %d in call to DeviceIoControl\n", GetLastError());
/*
	for (int i = 0; i < 512; i++)
		answer[i] = i + 1;
*/

	sprintf(buffer, "%s", "this is the first time I write data into Driver\r\n");
	WriteFile(hdevice, buffer, strlen(buffer), &junk, NULL);

	ZeroMemory(buffer, 1024);
	if (ReadFile(hdevice, buffer, 1024, &junk, NULL) != 0)
	{
		buffer[junk] = 0;
		//MessageBoxW(NULL,answer, NULL, 0);
		printf("%s",buffer);
	} else
		printf("Error %d in call to ReadFile\n", GetLastError());


	sprintf(buffer, "%s", "this is the second time I write data into Driver\r\n");
	WriteFile(hdevice, buffer, strlen(buffer), &junk, NULL);

	ZeroMemory(buffer, 1024);
	if (ReadFile(hdevice, buffer, 1024, &junk, NULL) != 0)
	{
		buffer[junk] = 0;
		//MessageBoxW(NULL,answer, NULL, 0);
		printf("%s",buffer);
	} else
		printf("Error %d in call to ReadFile\n", GetLastError());

	for (int i = 0; i < 1024; i++)
		buffer[i] = i + 1;
	buffer[1023] = '\n';
	WriteFile(hdevice, buffer, strlen(buffer), &junk, NULL);

	ZeroMemory(buffer, 1024);
	if (ReadFile(hdevice, buffer, 1024, &junk, NULL) != 0)
	{
		buffer[junk] = 0;
		//MessageBoxW(NULL,answer, NULL, 0);
		printf("%s",buffer);
	} else
		printf("Error %d in call to ReadFile\n", GetLastError());

	CloseHandle(hdevice);
	return 0;
}


